/**
 * @description 用户画像》编辑用户
 *@author yhw
 */
import React, {
  Component,
  PropTypes
} from 'react';
import {
  openidQuery,
  activeQuery,
  productQuery,
  valueQuery,
  getUserList,
  addUserInfo,
  updateUserInfo,
  deleteUser,
  updateUserPassword
} from '../services/UserEdit';
import {
  parse
} from 'qs';
import error from '../utils/tips';

import LineMarker from '../components/Charts/LineMarker';
import LineMarker1 from '../components/Charts/LineMarker1';
import LineMarker2 from '../components/Charts/LineMarker2';

export default {

  namespace: 'UserEdit',

  state: {
    openidData: {},
    openidLoading: false,

    activeLoading: false,
    productLoading: false,
    valueLoading: false,
    chartLineData: null,

    getUserListData: {
      "detail": [],
      "totalRecordSize": 0
    },
    getUserListLoading: false,

    updateUserInfoLoading: false,
    updateUserInfoResult: false,
  },

  subscriptions: {},

  effects: { * openidQuery({
      payload
    }, {
      call,
      put
    }) {

      yield put({
        type: 'openidLoading'
      });

      const {
        data
      } = yield call(openidQuery, parse(payload));

      if (data) {

        yield put({
          type: 'openidQuerySuccess',
          payload: {
            openidData: data.data,
          },
        });
      } else {
        error("获取用户信息失败");
      }
    },
    //===================================
    * activeQuery({
      payload
    }, {
      call,
      put
    }) {

      yield put({
        type: 'activeLoading'
      });

      const {
        data
      } = yield call(activeQuery, parse(payload));

      if (data) {

        let temp = {
          legend: [],
          name: "活跃度",
          category: [],
          value: [],
          markLine: false,
          max: 1
        };

        data.data && data.data.map(function(item, index) {
          temp.category.push(item.name);
          temp.value.push(item.value);
        });

        yield put({
          type: 'activeQuerySuccess',
          payload: temp
        });
      } else {
        error("获取用户个人活跃度失败");
      }
    },
    //===================================
    * productQuery({
      payload
    }, {
      call,
      put
    }) {

      yield put({
        type: 'productLoading'
      });

      const {
        data
      } = yield call(productQuery, parse(payload));

      if (data) {

        let temp = {
          legend: [],
          name: "产品喜好",
          category: [],
          markLine: false,
          max: 1
        };
        data.data && data.data.map(function(item, index) {
          temp.legend.push(item.name);

          temp["value" + index] = [];
          item.value.map(function(item2) {
            index == 0 && temp.category.push(item2.date);
            temp["value" + index].push({
              name: item.name,
              value: item2.count
            });
          });
        });

        yield put({
          type: 'productQuerySuccess',
          payload: temp
        });
      } else {
        error("获取用户产品喜好失败");
      }
    },
    //===================================
    * valueQuery({
      payload
    }, {
      call,
      put
    }) {

      yield put({
        type: 'valueLoading'
      });

      const {
        data
      } = yield call(valueQuery, parse(payload));

      if (data) {

        let temp = {
          legend: [],
          name: "用户价值",
          category: [],
          value: [],
          markLine: false,
          max: 1
        };
        data.data && data.data.map(function(item, index) {
          temp.category.push(item.name);
          temp.value.push(item.value);
        });

        yield put({
          type: 'valueQuerySuccess',
          payload: temp
        });
      } else {
        error("获取用户价值失败");
      }
    },
    //===================================
    * getUserList({
      payload
    }, {
      call,
      put
    }) {

      yield put({
        type: 'getUserListLoading'
      });

      const {
        data
      } = yield call(getUserList, parse(payload));

      if (data) {

        yield put({
          type: 'getUserListSuccess',
          payload: data.data
        });
      } else {
        error("获取用户列表失败");
      }
    },
    //===================================
    * addUserInfo({
      payload
    }, {
      call,
      put
    }) {

      yield put({
        type: 'addUserInfoLoading'
      });

      const {
        data
      } = yield call(addUserInfo, parse(payload));

      if (data) {
        yield put({
          type: 'addUserInfoSuccess',
          payload: data
        });
      } else {
        error("添加用户失败");
      }
    },
    //===================================
    * updateUserInfo({
      payload
    }, {
      call,
      put
    }) {

      yield put({
        type: 'updateUserInfoLoading'
      });

      const {
        data
      } = yield call(updateUserInfo, parse(payload));

      if (data) {
        yield put({
          type: 'updateUserInfoSuccess',
          payload: data
        });
      } else {
        error("更新用户失败");
      }
    },
    //===================================
    * deleteUser({
      payload
    }, {
      call,
      put
    }) {

      yield put({
        type: 'deleteUserLoading'
      });

      const {
        data
      } = yield call(deleteUser, parse(payload));

      if (data) {
        yield put({
          type: 'deleteUserSuccess',
          payload: data
        });
      } else {
        error("删除用户失败");
      }
    },
    //===================================
    * updateUserPassword({
      payload
    }, {
      call,
      put
    }) {

      yield put({
        type: 'updateUserPasswordLoading'
      });

      const {
        data
      } = yield call(updateUserPassword, parse(payload));

      if (data) {
        yield put({
          type: 'updateUserPasswordSuccess',
          payload: data
        });
      } else {
        error("修改用户密码失败");
      }
    },

  },

  reducers: {
    openidLoading(state) {
      return {...state,
        openidLoading: true
      };
    },
    openidQuerySuccess(state, action) {
      return {...state,
        ...action.payload,
        openidLoading: false
      };
    },
    //===================================
    activeLoading(state) {
      return {...state,
        activeLoading: true
      };
    },
    activeQuerySuccess(state, action) {
      return {...state,
        ...action.payload,
        activeLoading: false,
        chartLineData: (<LineMarker data={action.payload} width={'100%'} height={400} />)
      };
    },
    //===================================
    productLoading(state) {
      return {...state,
        productLoading: true
      };
    },
    productQuerySuccess(state, action) {
      return {...state,
        ...action.payload,
        productLoading: false,
        chartLineData: (<LineMarker data={action.payload} width={'100%'} height={400} />)
      };
    },
    //===================================
    valueLoading(state) {
      return {...state,
        valueLoading: true
      };
    },
    valueQuerySuccess(state, action) {
      return {...state,
        ...action.payload,
        valueLoading: false,
        chartLineData: (<LineMarker data={action.payload} width={'100%'} height={400} />)
      };
    },
    //===================================
    getUserListLoading(state) {
      return {...state,
        getUserListLoading: true
      };
    },
    getUserListSuccess(state, action) {
      return {...state,
        ...action.payload,
        getUserListLoading: false,
        getUserListData: action.payload
      };
    },
    //===================================
    addUserInfoLoading(state) {
      return {...state,
        updateUserInfoLoading: true
      };
    },
    addUserInfoSuccess(state, action) {

      if (!action.payload.data) {
        error("添加用户信息失败");
      }

      return {...state,
        ...action.payload,
        updateUserInfoLoading: false,
        updateUserInfoResult: !!action.payload.data
      };
    },
    //===================================
    updateUserInfoLoading(state) {
      return {...state,
        updateUserInfoLoading: true
      };
    },
    updateUserInfoSuccess(state, action) {

      if (!action.payload.data) {
        error("修改用户信息失败");
      }

      return {...state,
        ...action.payload,
        updateUserInfoLoading: false,
        updateUserInfoResult: !!action.payload.data
      };
    },
    //===================================
    deleteUserLoading(state) {
      return {...state,
        deleteUserLoading: true
      };
    },
    deleteUserSuccess(state, action) {

      if (!action.payload.data) {
        error("删除用户失败");
      }

      return {...state,
        ...action.payload,
        deleteUserLoading: false
      };
    },
    //===================================
    updateUserPasswordLoading(state) {
      return {...state,
        updateUserPasswordLoading: true
      };
    },
    updateUserPasswordSuccess(state, action) {
      return {...state,
        ...action.payload,
        updateUserPasswordLoading: false
      };
    },

  },
};